//
//  LDSHomeViewController.m
//  TestCategory
//
//  Created by kai_leedarson on 2017/10/30.
//Copyright © 2017年 maple_leedarson. All rights reserved.
//

#import "LDSHomeViewController.h"
#import "LDSHomeViewControllerViewModel.h"
#import "LDSCollectionViewModel.h"
#import "LDSHeadStyleView.h"
#import "DCPathButton.h"
#import "UINavigationController+Cloudox.h"
#import "UIBarButtonItem+CustomUI.h"
#import "LDSPresentAnimation.h"
//#import "LDSDropDownViewController.h"
#import "YYBNavigationTitleView.h"

@interface LDSHomeViewController ()
@property(nonatomic, strong)  LDSHomeViewControllerViewModel *viewControllerViewModel;//必须子类化
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) LDSCollectionViewModel *collectionViewModel;//选择性子类化
@property(nonatomic, strong) UICollectionViewFlowLayout *collectionLayout;
@property(nonatomic, strong) NSMutableArray *dataSource;//cell 数据源
@property(nonatomic, strong) LDSHeadStyleView *headView;
@property(nonatomic , strong) DCPathButton *pathAnimationView;
@property(nonatomic, strong) YYBNavigationTitleView *navigationTitleView;
//@property(nonatomic, strong) LDSDropDownViewController *dropDownListVC;//下拉菜单
@end

@implementation LDSHomeViewController

#pragma mark - lazy init

- (YYBNavigationTitleView *)navigationTitleView {
    if (_navigationTitleView == nil) {
        _navigationTitleView = [[YYBNavigationTitleView alloc] init];
        _navigationTitleView.needsLoadingView = NO;
        _navigationTitleView.accessoryType = YYBNavigationTitleViewAccessoryTypeDisclosureIndicator;
        _navigationTitleView.delegate = (id<YYBNavigationTitleViewDelegate>)self;
        _navigationTitleView.userInteractionEnabled = YES;
        _navigationTitleView.title = @"My Home";
    }
    return _navigationTitleView;
}

- (LDSHomeViewControllerViewModel *)viewControllerViewModel {
    if (_viewControllerViewModel== nil) {
        _viewControllerViewModel = [[LDSHomeViewControllerViewModel alloc] init];
        _viewControllerViewModel.delegate = (id<LDSViewControllerViewModelDelegate>) self;
    }
    return _viewControllerViewModel;
}

- (LDSCollectionViewModel *)collectionViewModel {
    if (_collectionViewModel == nil) {
        _collectionViewModel = [[LDSCollectionViewModel alloc] init];
        _collectionViewModel.delegate = (id<LDSCollectionViewModelDelegate>)self;
        //接管collectionView
        [_collectionViewModel handleWithCollection:self.collectionView];
        
    }
    return _collectionViewModel;
}

- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 161, self.view.frame.size.width, self.view.frame.size.height - 161)
                                             collectionViewLayout:self.collectionLayout];
        _collectionView.backgroundColor = [UIColor whiteColor] ;
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

- (UICollectionViewLayout *)collectionLayout {
    if (_collectionLayout == nil) {
        _collectionLayout = [[UICollectionViewFlowLayout alloc] init];
        [_collectionViewModel handleWithCollectionLayout:_collectionLayout];
        CGFloat photoSizeW = [UIScreen mainScreen].bounds.size.width/4;
        CGFloat photoSizeH = [UIScreen mainScreen].bounds.size.width/4-5*([UIScreen mainScreen].bounds.size.width)/768+25;
        //[_collectionLayout setMinimumInteritemSpacing:([[UIScreen mainScreen] bounds].size.width - 375) > 0 ? 8.0 : 6.0];
        //[_collectionLayout setMinimumLineSpacing:16.0];
        [_collectionLayout setItemSize:(CGSize){photoSizeW, photoSizeH}];
        [_collectionLayout setHeaderReferenceSize:(CGSize){[[UIScreen mainScreen] bounds].size.width, 23.0f}];
    }
    return _collectionLayout;
}

- (LDSHeadStyleView *)headView {
    if (_headView == nil) {
        _headView = [[LDSHeadStyleView alloc] initWithFrame:CGRectMake(0, -64, [UIScreen mainScreen].bounds.size.width, 215)];
    }
    return _headView;
}

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSString *)barItemTitle {
    return @"";
}
//
//- (LDSDropDownViewController *)dropDownListVC {
//    if (_dropDownListVC == nil) {
//        _dropDownListVC = [[LDSDropDownViewController alloc] init];
//        _dropDownListVC.modalPresentationStyle = UIModalPresentationPopover;
//        _dropDownListVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionLeft; //箭头方向
//    }
//    return _dropDownListVC;
//}

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageNamed:@"home_img_bg"];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:imageView];
    [self.viewControllerViewModel loadFetchData];
    [self.view addSubview:self.headView];
    
    [self ConfigureDCPathButton];
    self.navigationItem.titleView = self.navigationTitleView;
    
//    UIButton *abc = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
//    abc.backgroundColor = [UIColor redColor];
//    abc.center = self.view.center;
//    
//    [self.view addSubview:abc];
//    [abc addTarget:self action:@selector(navigaCenterClicked:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (UIBarButtonItem *)leftBarButtonItem {
    return [UIBarButtonItem leftBarItemWithImage:[UIImage imageNamed:@"nav_icon_menu"] highlightedImage:[UIImage imageNamed:@"nav_icon_menu"] target:self action:nil];
}

- (UIBarButtonItem *)rightBarButtonItem {
    return [UIBarButtonItem rightBarItemWithImage:[UIImage imageNamed:@"nav_icon_menu"] highlightedImage:[UIImage imageNamed:@"nav_icon_menu"] target:self action:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNeedsNavigationBackground:0.0];
}

#pragma mark - <QMUINavigationTitleViewDelegate>
- (void)didChangedActive:(BOOL)active forTitleView:(YYBNavigationTitleView *)titleView {
    if (active) {
//        [self navigaCenterClicked:titleView];
    }
}

//#pragma mark---- UIPopoverPresentationControllerDelegate
//- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
//    return UIModalPresentationNone; //不显示modal效果
//}
//
//- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
//    self.navigationTitleView.active = NO;
//    return YES;
//}
//
//- (void)navigaCenterClicked:(UIView *)sender {
//    self.dropDownListVC.popoverPresentationController.sourceView = sender; //rect参数是以view的左上角为坐标原点（0，0）
//    self.dropDownListVC.popoverPresentationController.sourceRect = sender.bounds;
//    self.dropDownListVC.popoverPresentationController.delegate = (id <UIPopoverPresentationControllerDelegate>)self;
//    self.dropDownListVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp; //箭头方向
//    [self presentViewController:self.dropDownListVC animated:YES completion:nil];
//}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

#pragma mark - LDSCollectionViewModelDelegate
- (void)lds_collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}

//- (UIEdgeInsets)lds_collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
//    return UIEdgeInsetsMake(14, 14,14, 14);
//}

#pragma mark - SecondVCViewModelDelegate
- (void)lds_didReceiveDataWithDataSource:(NSArray *)dataSource andSectionDataSource:(NSArray *)sectionDataSource {
    self.dataSource = [dataSource mutableCopy];
    [self.collectionViewModel handleWithDataSource:self.dataSource];
    [self.collectionView reloadData];
}

- (void)ConfigureDCPathButton
{
    // Configure center button
    //
    DCPathButton *dcPathButton = [[DCPathButton alloc]initWithCenterImage:[UIImage imageNamed:@"tab_icon_home_add"]
                                                           hilightedImage:[UIImage imageNamed:@"tab_icon_home_add"]];
    _pathAnimationView = dcPathButton;
    
    dcPathButton.delegate = (id<DCPathButtonDelegate>)self;
    
    // Configure item buttons
    //
    DCPathItemButton *itemButton_1 = [[DCPathItemButton alloc]initWithImage:[UIImage imageNamed:@"home_add_icon_adddevice"]
                                                           highlightedImage:[UIImage imageNamed:@"home_add_icon_adddevice"]
                                                                      title:@"添加设备"];
    
    DCPathItemButton *itemButton_2 = [[DCPathItemButton alloc]initWithImage:[UIImage imageNamed:@"home_add_icon_addscene"]
                                                           highlightedImage:[UIImage imageNamed:@"home_add_icon_addscene"]
                                                                      title:@"添加场景"];
    
    // Add the item button into the center button
    [dcPathButton addPathItems:@[itemButton_1, itemButton_2]];
    
    [self.view addSubview:dcPathButton];
    
}
@end
